﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Data;
using System.Collections;
using System.Reflection;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace ElectronicWMSA.Comm.ClassFiles
{
    public static class MethodClass
    {
        #region
        #endregion

        #region DataGridView To Table
        /// <summary>
        /// DataGridView To Table
        /// </summary>
        /// <param name="dgv">DataGridView</param>
        /// <returns></returns>
        public static DataTable GetDgvToTable(DataGridView dgv)
        {
            DataTable dt = new DataTable();
            for (int count = 0; count < dgv.Columns.Count; count++)
            {
                DataColumn dc = new DataColumn(dgv.Columns[count].Name.ToString());
                dt.Columns.Add(dc);
            }
            for (int count = 0; count < dgv.Rows.Count; count++)
            {
                DataRow dr = dt.NewRow();
                for (int countsub = 0; countsub < dgv.Columns.Count; countsub++)
                {
                    dr[countsub] = dgv.Rows[count].Cells[countsub].Value.ToString();
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }
        #endregion

        #region 文件是否打开
        /// <summary>
        ///  文件是否打开
        /// </summary>
        /// <param name="fileName">fileName</param>
        /// <returns></returns>
        public static bool FileOpenStauts(string fileName)
        {
            bool falg = false;
            try
            {
                File.Move(fileName, fileName);
                falg = true;
            }
            catch
            {
            }
            return falg;
        }
        #endregion

        #region 检测文件被占用
        [DllImport("kernel32.dll")]
        public static extern IntPtr _lopen(string lpPathName, int iReadWrite);
        [DllImport("kernel32.dll")]
        public static extern bool CloseHandle(IntPtr hObject);
        public const int OF_READWRITE = 2;
        public const int OF_SHARE_DENY_NONE = 0x40;
        public static IntPtr HFILE_ERROR = new IntPtr(-1);
        /// <summary> 
        /// 检测文件被占用 
        /// </summary>
        /// <param name="FileNames">要检测的文件路径</param>
        /// <returns></returns>
        public static bool CheckFiles(string FileNames)
        {
            if (!File.Exists(FileNames))
            {
                return true;
            }
            IntPtr vHandle = _lopen(FileNames, OF_READWRITE | OF_SHARE_DENY_NONE);
            if (vHandle == HFILE_ERROR)
            {
                return false;
            }
            CloseHandle(vHandle);
            return true;
        }
        #endregion

        #region List To DataTable
        /// <summary>
        /// List To DataTable
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collection"></param>
        /// <returns></returns>
        public static DataTable ToDataTable<T>(IEnumerable<T> collection)
        {
            var props = typeof(T).GetProperties();
            var dt = new DataTable();
            dt.Columns.AddRange(props.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray());
            if (collection.Count() > 0)
            {
                for (int i = 0; i < collection.Count(); i++)
                {
                    ArrayList tempList = new ArrayList();
                    foreach (PropertyInfo pi in props)
                    {
                        object obj = pi.GetValue(collection.ElementAt(i), null);
                        tempList.Add(obj);
                    }
                    object[] array = tempList.ToArray();
                    dt.LoadDataRow(array, true);
                }
            }
            return dt;
        }
        #endregion

        #region 两个字符串是否存在相同项
        /// <summary>
        /// 两个字符串是否存在相同项
        /// </summary>
        /// <returns>bool</returns>
        public static bool SameItem(string str1, string str2)
        {
            bool fal = false;
            //arr1.Where(c => !arr2.Contains(c)).ToArray();//找出不同的元素
            if (str1.Intersect(str2).ToArray().Length > 0)//找出相同元素(即交集)
                fal = true;
            return fal;
        }
        #endregion

        #region 数字是否连续
        /// <summary>
        /// 数字是否连续
        /// </summary>
        public static bool WhetherContinuous(string str)
        {
            bool flag = true;
            string[] arry = str.Split(',');
            for (int i = 0; i < arry.Length - 1; i++)
            {
                if (i + 1 < arry.Length - 1)
                {
                    int temp = Convert.ToInt32(arry[i]) - Convert.ToInt32(arry[i + 1]);
                    if (temp != 1)
                    {
                        flag = false; break;
                    }
                }
            }
            return flag;
        }
        #endregion

        #region 二进制文件转换
        /// <summary>
        /// 将文件转换成二进制流数据
        /// </summary>
        /// <param name="path">文件的完整路径(包括文件名)</param>
        /// <returns></returns>
        public static byte[] GetByteOfFile(string path)
        {
            FileInfo fi = new FileInfo(path);
            FileStream fs = fi.OpenRead();
            byte[] bytes = new byte[fs.Length];
            fs.Read(bytes, 0, Convert.ToInt32(fs.Length));
            return bytes;
        }

        /// <summary>
        /// 将二进制流数据保存为本地文件,如果文件已经存在将删除并重新创建
        /// </summary>
        /// <param name="fileName">文件的完整路径</param>
        /// <param name="file">二进制流数据</param>
        public static void SaveFile(string fileName, byte[] file)
        {
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
            FileStream fs = new FileStream(fileName, FileMode.CreateNew);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(file, 0, file.Length);
            bw.Close();
            fs.Close();
        }
        #endregion
    }
}
